//
//  ios_Image.mm
//  Prism
//
//  Created by Bas du Pre on 20-05-11.
//  Copyright 2011 UU. All rights reserved.
//

// iOS specific implementation

#include "global.h"
#if PRISM_TARGET_SDL
#include "sdl/sdl_global.h"

#include "graphics/Texture.h"

#include <iostream>

void Prism::Graphics::Texture::native_init() {
    ///\todo textures should be cached
    
    sdlSurface = 0;
    textureFormat = GL_ZERO;
    glTexture = GL_ZERO;
    
    if ((sdlSurface = IMG_Load(file()->filename()))) {
        // Check that the image's width is a power of 2
        if ((sdlSurface->w & (sdlSurface->w - 1)) != 0 ) {
            String filename = file()->filename();
            PRISM_LOG_QUIET("warning: %_: width is not a power of 2", &filename);
        }
        // Also check if the height is a power of 2
        if ((sdlSurface->h & (sdlSurface->h - 1)) != 0 ) {
            String filename = file()->filename();
            PRISM_LOG_QUIET("warning: %_: height is not a power of 2", &filename);
        }
        //get number of channels in the SDL surface
        GLint nofcolors = sdlSurface->format->BytesPerPixel;
        //contains an alpha channel
        if(nofcolors == 4)
        {
            if(sdlSurface->format->Rmask == 0x000000ff)
                textureFormat = GL_RGBA;
            else
                textureFormat = GL_BGRA;
        }
        else if(nofcolors==3) //no alpha channel
        {
            if(sdlSurface->format->Rmask == 0x000000ff)
                textureFormat = GL_RGB;
            else
                textureFormat = GL_BGR;
        }
        else
        {
            PRISM_LOG_QUIET("warning: the image is not truecolor.. this will break");
        }

        // Have OpenGL generate a texture object handle for us
        glGenTextures(1, &glTexture);
        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, glTexture);
        
        glTexImage2D(GL_TEXTURE_2D, 0, nofcolors, sdlSurface->w, sdlSurface->h, 0,
                     textureFormat, GL_UNSIGNED_BYTE, sdlSurface->pixels);
        // Set the texture's stretching properties
        // anti-alias:
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        // Prevent artefacts at the edges when in antialias
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    }
    else {
        String filename = file()->filename();
        PRISM_LOG_QUIET("SDL could not load '%_': %s", &filename, SDL_GetError());
        //SDL_Quit();
    }
}

void Prism::Graphics::Texture::native_setAntiAlias(bool antiAlias) {
    glBindTexture(GL_TEXTURE_2D, glTexture);
    
    if (antiAlias) {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    } else {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    }
}

void Prism::Graphics::Texture::native_deInit() {
    // Free the SDL_Surface only if it was successfully created
    if (sdlSurface) {
        SDL_FreeSurface(sdlSurface);
    }
    glDeleteTextures(1, &glTexture);
}

Prism::Size Prism::Graphics::Texture::native_size() {    
    Prism::Size size;
    
    size.width = sdlSurface->w;
    size.height = sdlSurface->h;
    
    return size;
}


#endif